package com.edu.demo.controller;

import com.edu.demo.entity.MediumTerm;
import com.edu.demo.entity.Project;
import com.edu.demo.entity.Teacher;
import com.edu.demo.pojo.AjaxResult;
import com.edu.demo.service.IProjectService;
import com.edu.demo.service.ITaskService;
import com.edu.demo.utils.FileUtils;
import com.edu.demo.utils.SecurityUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;



import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private ITaskService taskService;

    /**
     * 管理员、老师项目列表
     * @param
     * @return
     */
    @GetMapping("/list/admin")
    @PreAuthorize("@ps.hasPerm('sys:project:list')")
    public AjaxResult projectList(@RequestParam int index,@RequestParam int size){
//        Integer index = para.get("index"),size = para.get("size");
//        Integer status = para.get("status"),mid_term = para.get("midTerm"),conclusion = para.get("conclusion"),revoke = para.get("revoke");
//        Project project = Project.builder().status(status).midTerm(mid_term).conclusion(conclusion).revoke(revoke).build();
        AjaxResult success = AjaxResult.success();
//        Project project = new Project();

        List<Project> projectList = projectService.projectList(index, size ,success);
        success.put("data",projectList);
        return success;
    }

    /**
     * 项目各个状态数量
     * @return
     */
    @GetMapping("/info")
    @PreAuthorize("@ps.hasPerm('sys:project:list')")
    public AjaxResult projectInfo(){
        return AjaxResult.success(projectService.projectInfo());
    }

    /**
     * 按条件查询项目
     * @param project
     * @return
     */
    @PostMapping("/search")
    @PreAuthorize("@ps.hasPerm('sys:project:search')")
    public AjaxResult searchList(@RequestBody Project project,@RequestParam int index,@RequestParam int size){
        AjaxResult success = AjaxResult.success();
        List<Project> projectList = projectService.queryProject(project,index,size, success);
         success.put("data",projectList);
         return success;
    }

    /**
     * 新添一个项目，一个项目多个task,只允许老师添加
     * @param project
     * @return
     */
    @PostMapping("/insert")
    @PreAuthorize("@ps.hasPerm('tea:project:insert')")
    public AjaxResult insertProject(@RequestBody Project project){
        try {
            if(Strings.isEmpty(project.getPhoto())){
                project.setPhoto(FileUtils.photoInCache());
            }
            project.setReleaseTime(new Date());
            int i = projectService.insertProject(project);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("新增项目服务出错！请联系管理员");
        }

    }

    /**
     * 浏览一个项目，访问量+1
     * @param id
     * @return
     */
    @GetMapping("/find")
    @PreAuthorize("@ps.hasPerm('common:project:query')")
    public AjaxResult selectOneProject(@RequestParam Integer id){
        try {
            Project project = projectService.queryProjectById(id);
            return AjaxResult.success(project);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务出错！");
        }

    }


    /**
     * 老师查看自己的项目
     * @param index
     * @param size
     * @return
     */
    @GetMapping("/list/teacher")
    @PreAuthorize("@ps.hasPerm('tea:project:teacher')")
    public AjaxResult projectsOfTeacher(@RequestParam Integer index,@RequestParam Integer size){
        AjaxResult success = AjaxResult.success();
        List<Project> projectList = projectService.projectsOfTeacher(index, size,success);
        success.put("data",projectList);
        return success;
    }

    /**
     * 老师查找自己的项目
     * @param index
     * @param size
     * @return
     */
    @PostMapping("/list/teacher/search")
    @PreAuthorize("@ps.hasPerm('tea:project:teacher')")
    public AjaxResult projectSearchMine(@RequestBody Project project,@RequestParam int index,@RequestParam int size){
        Teacher teacher = SecurityUtils.getTeacher();
        project.settId(teacher.getId());
        AjaxResult ajaxResult = searchList(project, index, size);
        return ajaxResult;
    }

    /**
     * 学生查询自己参加的项目
     * @param index
     * @param size
     * @return
     */
    @GetMapping("/list/student")
    @PreAuthorize("@ps.hasPerm('stu:project:student')")
    public AjaxResult projectsOfStudent(@RequestParam Integer index,@RequestParam Integer size){
        AjaxResult success = AjaxResult.success();
        List<Project> projectList = projectService.projectsOfStudent(index, size, success);
        if(projectList.isEmpty()){
            return AjaxResult.error("没有在职的项目");
        }
        success.put("data",projectList);
        return success;
    }

    /**
     * 教师更新一个项目
     * @param project
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("@ps.hasPerm('tea:project:update')")
    public AjaxResult updateProject(@RequestBody Project project){
        projectService.updateAProject(project);

        return AjaxResult.success();
    }

    /**
     * 教师将项目停止招聘
     * @param
     * @return
     */
    @GetMapping("/stop")
    @PreAuthorize("@ps.hasPerm('tea:project:update')")
    public AjaxResult stopProject(@RequestParam Integer projectId){
        Project project = projectService.queryProjectById(projectId);
        boolean b = project.getStatus() == 0;
        if(b){
            project.setStatus(1); //状态1代表完成招聘
            projectService.updateById(project);
        }
        return AjaxResult.success();
    }


    /**
     * 学生查询某个学院正在招聘的项目
     */
    @GetMapping("/list/recruit")
    @PreAuthorize("@ps.hasPerm('stu:project:recruit')")
    public AjaxResult isRecruitIng(@RequestParam Integer department,@RequestParam Integer index,@RequestParam Integer size){
        try {
            AjaxResult success = AjaxResult.success();
            List<Project> projectList = projectService.projectsOfRecruiting(department,success,index,size);
            success.put("data",projectList);
            return success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务出错");
        }

    }


}
